

let EventEmitter = require('./event_emitter');

class Fps extends EventEmitter{

    constructor() {
        super();
        this._accumulateTime = 0;
        this._lastTime = Date.now(); //
        this._frameCount = 0;

        this._fps = 0;
        this._dt = 0;
    }

    getFps(){
        return this._fps;
    }

    get dt(){
        return this._dt;
    }

    calFpsAndDt() {
        this._frameCount++;

        let cur_time = Date.now();
        let dt_ms = cur_time - this._lastTime;

        this._accumulateTime += dt_ms;
        if(this._accumulateTime >= 1000){
            this._fps = this._frameCount;
            this._frameCount = 0;
            this._accumulateTime -= 1000;
            this.emit(Fps.EventUpdate,this._fps);
        }

        this._lastTime = cur_time;
        this._dt = dt_ms / 1000;
        return this._dt;
    }
}

Fps.EventUpdate = 'event_update';

module.exports = Fps;
